<?php
/**
 * 在线更新 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_comment.php 511 2010-06-18 06:04:40Z lisijie86 $
*/

include_once JBLOG_INC.'class_snoopy.php';

//检查更新
if ( $ac == 'check' ) {
	$client = new Snoopy();
	$client->fetch( UPDATE_SOURCE . '/' . str_replace(' ','_',JBLOG_VERSION) . '/' . 'update-list.txt');
	$output = array();
	if ( !file_exists(DATA_ROOT.'update/lasttime.php') ) {
		$lasttime = JBLOG_RELEASE;
	} else {
		list(, $lasttime) = file(DATA_ROOT.'update/lasttime.php');
	}
	if ( false !== strpos($client->response_code,'200') && $client->results ) {
		$result = gb2utf8($client->results);
		$uplist = explode("\n", str_replace("\r", '', $result));
		foreach ( $uplist as $line ) {
			if ( empty($line) || preg_match('/^\/\//', $line) ) {
				continue;
			}
			list($date, $issafe, $msg) = array_map('trim',explode(',', $line));
			if ( $date > $lasttime ) {
				$output['upvers'][] = array(
					'date' => substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2),
					'issafe' => $issafe ? 1 : 0,
					'msg' => $msg
				);
				$output['lasttime'] = $date;
				$output['upitems'] .= $output['upitems'] ? ','.$date : $date;
			}
		}
	}
	ajax_out($output);
}
//跳过更新
elseif ( $ac == 'skip' ) {
	initGP('lasttime');
	file_put_contents(DATA_ROOT.'update/lasttime.php', "<?php die;?>\n{$lasttime}");
	die('success');
}
//获取更新文件列表
elseif ( $ac == 'getlist' ) {
	initGP('upitems');
	$upitems = explode(',', $upitems);
	$filelist = array();
	$client = new Snoopy();
	foreach ( $upitems as $item ) {
		if ( $item ) {
			$client->fetch(UPDATE_SOURCE . '/' . str_replace(' ','_',JBLOG_VERSION) . '/' . $item . '.list.txt');
			if ( false !== strpos($client->response_code,'200') ) {
				$result = str_replace("\r","\n",$client->results);
				$result = explode("\n", $result);
				foreach ( $result as $value ) {
					if ( !empty($value) ) {
						$value = explode("\t", $value); //file.php	md5_hash
						$url = UPDATE_SOURCE . '/' . str_replace(' ','_',JBLOG_VERSION) . '/' . $item . '/' . $value[0];
						$filelist[] = array('file'=>$value[0],'url'=>$url,'hash'=>$value[1]);
					}
				}
			}
		}
	}
	if ( file_exists(DATA_ROOT.'update/upcache.php') ) {
		include_once DATA_ROOT.'update/upcache.php';
		$tempdir = $_tempdir;
	}
	if ( empty($tempdir) || !is_dir(DATA_ROOT . 'update/'.$tempdir) ) {
		$tempdir = get_date(NOW,'Ymd').'_'.uniqid();
		make_dir(DATA_ROOT . 'update/'.$tempdir);
	}
	$upcache = "<?php\n\$_tempdir='{$tempdir}';\n\$_lasttime='".max($upitems)."';\n\$_count=".count($filelist).";\n\$_filelist = ".var_export($filelist, true)."\n?>";
	file_put_contents(DATA_ROOT.'update/upcache.php', $upcache);
	include admin_tpl('update');
}
//下载更新文件
elseif ( $ac == 'downfile' ) {
	require_once DATA_ROOT.'update/upcache.php';
	initGP(array('id'),'G','int');
	if ( $id > $_count ) {
		die('done');
	}
	if ( isset($_filelist[$id]) ) {
		$client = new Snoopy();
		$client->fetch($_filelist[$id]['url']);
		if ( false !== strpos($client->response_code,'200') && !empty($client->results) ) {
			$file = DATA_ROOT."update/{$tempdir}/".$_filelist[$id]['file'];
			make_dir(dirname($file));
			file_put_contents($file, $client->results);
			if ( md5_file($file) == $_filelist[$id]['hash'] ) {
				die('ok');
			} else {
				die('failed');
			}
		} else {
			die('failed');
		}
	}
}
//应用更新
elseif ( $ac == 'update' ) {
	require_once DATA_ROOT.'update/upcache.php';
	
}
?>